// utils/system.js

const getBaseSystemInfo = () => {
  try {
    const systemInfo = wx.getSystemInfoSync()
    return systemInfo
  } catch (error) {
    console.error('获取系统信息失败:', error)
    return {}
  }
}

export const system = {
  /**
   * 获取完整的系统信息
   */
  getSystemInfo() {
    return getBaseSystemInfo()
  },

  /**
   * 获取状态栏高度
   */
  getStatusBarHeight() {
    const systemInfo = getBaseSystemInfo()
    return systemInfo.statusBarHeight || 0
  },

  /**
   * 获取导航栏高度
   */
  getNavBarHeight() {
    const statusBarHeight = this.getStatusBarHeight()
    const systemInfo = getBaseSystemInfo()
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
    const navBarHeight = (menuButtonInfo.top - statusBarHeight) * 2 + menuButtonInfo.height
    return navBarHeight
  },

  /**
   * 获取胶囊按钮信息
   */
  getMenuButtonInfo() {
    return wx.getMenuButtonBoundingClientRect()
  },

  /**
   * 获取屏幕宽度
   */
  getScreenWidth() {
    const systemInfo = getBaseSystemInfo()
    return systemInfo.screenWidth || 0
  },

  /**
   * 获取屏幕高度
   */
  getScreenHeight() {
    const systemInfo = getBaseSystemInfo()
    return systemInfo.screenHeight || 0
  },

  /**
   * 获取可使用窗口宽度
   */
  getWindowWidth() {
    const systemInfo = getBaseSystemInfo()
    return systemInfo.windowWidth || 0
  },

  /**
   * 获取可使用窗口高度
   */
  getWindowHeight() {
    const systemInfo = getBaseSystemInfo()
    return systemInfo.windowHeight || 0
  },

  /**
   * 获取安全区域
   */
  getSafeArea() {
    const systemInfo = getBaseSystemInfo()
    return systemInfo.safeArea || {}
  },

  /**
   * 获取设备信息
   */
  getDeviceInfo() {
    const systemInfo = getBaseSystemInfo()
    return {
      brand: systemInfo.brand,
      model: systemInfo.model,
      system: systemInfo.system,
      platform: systemInfo.platform
    }
  },

  platform: {
    /**
     * 判断是否是iOS设备
     */
    isIOS() {
      const systemInfo = getBaseSystemInfo()
      return systemInfo.platform === 'ios'
    },

    /**
     * 判断是否是Android设备
     */
    isAndroid() {
      const systemInfo = getBaseSystemInfo()
      return systemInfo.platform === 'android'
    }
  }
}